package view;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.passby.R;

import data.PassByItem;
import data.User;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

public class MapBubbleItem<Item extends OverlayItem> extends FrameLayout {
	
	private LinearLayout layout;
	private ImageView image;
	private TextView title;
	private TextView content;
	private Drawable bg;
	private ImageView close;
	
	public MapBubbleItem(Context context, int balloonBottomOffset) {

		super(context);

		setPadding(10, 0, 10, balloonBottomOffset);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);

		setupView(context, layout);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		addView(layout, params);
	}
	
	protected void setupView(Context context, final ViewGroup parent) {
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.user_info_view, parent);
		
		title = (TextView) v.findViewById(R.id.user_info_name);
		content = (TextView) v.findViewById(R.id.user_info_item);
		image = (ImageView) v.findViewById(R.id.user_avatar_image);
		bg = v.findViewById(R.id.user_info_layout).getBackground();

		close = (ImageView) v.findViewById(R.id.user_info_close);
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				setVisibility(GONE);
			}
		});
		
	}
	
	public void setLayerItem(PassByOverlayItem item) {
		layout.setVisibility(VISIBLE);
		setBalloonData(item, layout);
	}
	
	protected void setBalloonData(PassByOverlayItem item, ViewGroup parent) {
		
		// map our custom item data to fields
		title.setText(item.getUser().username+":");
		PassByItem it = item.getUser().getLatestItem();
		if(it != null)
		{
			content.setText(it.content);
		}
		
		// get remote image from network.
		// bitmap results would normally be cached, but this is good enough for demo purpose.
		image.setImageResource(R.drawable.icon);
		new FetchImageTask() { 
	        protected void onPostExecute(Bitmap result) {
	            if (result != null) {
	            	image.setImageBitmap(result);
	            }
	        }
	    }.execute(item.getImageURL());
	}
	
	@Override
	public void setVisibility(int visibility)
	{
		super.setVisibility(visibility);
		layout.setVisibility(visibility);
	}
	
	public void setData(PassByItem item)
	{
		title.setText(item.user.username+":");
		content.setText(item.content);
	}

	private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
	    @Override
	    protected Bitmap doInBackground(String... arg0) {
	    	Bitmap b = null;
	    	try {
				 b = BitmapFactory.decodeStream((InputStream) new URL(arg0[0]).getContent());
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} 
	        return b;
	    }	
	}
	
	public void setAlpha(int alpha)
	{
		title.setTextColor(title.getTextColors().withAlpha(alpha));
		content.setTextColor(content.getTextColors().withAlpha(alpha));
		close.setAlpha(alpha);
		image.setAlpha(alpha);
		bg.setAlpha(alpha);
	}
}
